﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;

namespace School.SystemManage.WebPages.ArticlePage
{
    public partial class ArticleList : Base.PermissionBasePage
    {
        protected string menu = string.Empty;
        protected string subMenu = string.Empty;
        protected string categoryStr = string.Empty;
        string role = string.Empty;

        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {   
            categoryStr = Request.QueryString["category"];
            int category = int.Parse(categoryStr);

            SubMenu submenu = db.SubMenu.First(m => m.Categories == category);
            menu = db.SubMenu.First(m => m.Categories == submenu.ParentCategory).Name;
            subMenu = submenu.Name;

            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            int category = int.Parse(categoryStr);
            var data = from articles in db.Article
                       where articles.SubMenu.Categories == category
                       select new
                       {
                           articles.ArticleID,
                           articles.Title,
                           articles.IsMainNews,
                           articles.UpdateTime,
                           articles.AdminUsers.UserName,
                           articles.Remarks,
                           articles.IsChecked,
                           articles.IsEnabled,
                           CheckedUserName = articles.CheckedUserID == 0 ? "未审核" : db.AdminUsers.FirstOrDefault(u => u.UserID == articles.CheckedUserID).UserName
                       };

            rep_Article.DataSource = data;
            rep_Article.DataBind();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (RepeaterItem item in rep_Article.Items)
                {
                    CheckBox cb = (CheckBox)item.FindControl("cbChecked");
                    if (cb.Checked)
                    {
                        HiddenField hf = (HiddenField)item.FindControl("hfAid");
                        int aid = int.Parse(hf.Value);

                        //删除附件
                        var attachment = db.Attachment.Where(a => a.Article.ArticleID == aid);
                        if (attachment.Count() > 0)
                        {
                            foreach (var att in attachment)
                            {
                                db.DeleteObject(att);
                            }
                        }

                        //删除文章
                        var article = db.Article.First(a => a.ArticleID == aid);
                        db.DeleteObject(article);
                    }
                }

                db.SaveChanges();
                BindData();
            }
            catch (Exception ex)
            {
                msg.Text = "删除发生异常，原因：" + ex.Message;
            }
        }

        /// <summary>
        /// 获取文章审核状态
        /// </summary>
        /// <returns></returns>
        protected string GetCheckedStatue(byte statueCode)
        {
            string statueStr = string.Empty;
            switch (statueCode)
            {
                case 0:
                    statueStr = "未审核";
                    break;
                case 1:
                    statueStr = "已通过";
                    break;
                case 2:
                    statueStr = "未通过";
                    break;
                default:
                    break;
            }
            return statueStr;
        }

        protected void CheckSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (RepeaterItem item in rep_Article.Items)
                {
                    CheckBox cb = (CheckBox)item.FindControl("cbChecked");
                    if (cb.Checked)
                    {
                        HiddenField hf = (HiddenField)item.FindControl("hfAid");
                        int aid = int.Parse(hf.Value);

                        var article = db.Article.First(a => a.ArticleID == aid);
                        article.IsChecked = byte.Parse(rbtnCheck.SelectedValue);
                        article.Remarks = txbRemart.Text.Trim();
                        article.CheckedTime = DateTime.Now;
                        article.CheckedUserID = int.Parse(GetCookie("UserLoginID"));
                    }
                }

                db.SaveChanges();
                BindData();
            }
            catch (Exception ex)
            {
                msg.Text = "审核发生异常，原因：" + ex.Message;
            }
        }

        protected void rep_Article_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (role == "审核员")
                {
                    HyperLink link = (HyperLink)e.Item.FindControl("editButton");
                    link.Visible = false;
                }
                else
                {
                    string aid = ((HiddenField)e.Item.FindControl("hfAid")).Value;
                    HyperLink link = (HyperLink)e.Item.FindControl("editButton");
                    link.NavigateUrl = "ArticleEdit.aspx?action=edit&category=" + categoryStr + "&aid=" + aid;
                }
            }
        }

        #region 操作权限控制

        public override void SetEditDisable()
        {
            base.SetEditDisable();

            btnAddItem.Visible = false;
            btnDelItem.Visible = false;
            foreach (RepeaterItem item in rep_Article.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    HyperLink linkEdit = (HyperLink)item.FindControl("editButton");
                    linkEdit.Visible = false;
                }
            }
        }

        public override void SetReviewDisable()
        {
            base.SetReviewDisable();

            btnCheckItem.Visible = false;
        }

        public override void SetViewDisable()
        {
            base.SetViewDisable();          
        }

        #endregion
    }
}
